﻿using System;
using System.Windows.Forms;
using Bornander.UI;
using GeoFramework;
using LetsDoIt.BO;
using System.Drawing;

namespace LetsDoIt.Controls
{
	public class GpsDeviceItem : UserControl, IExtendedListItem
	{
        private Label lblGpsDeviceName;
        private PictureBoxEx picGpsIcon;

        private GpsDeviceInfo gpsinfo;
        public GpsDeviceInfo GpsDevice
		{
            get { return gpsinfo; }
            set { gpsinfo = value; }
		}

		public GpsDeviceItem()
		{
			InitializeComponent();
			SelectedChanged(false);
		}

        public GpsDeviceItem(GpsDeviceInfo gpsinfo)
		{
            this.gpsinfo = gpsinfo;
			InitializeComponent();

            switch (gpsinfo.Type)
            {
                case GpsDeviceInfo.GpsType.Bluetooth:
                    picGpsIcon.Image = Properties.Resources.gps_bluetooth;
                    break;
                case GpsDeviceInfo.GpsType.Internal:
                    picGpsIcon.Image = Properties.Resources.gps_internal;
                    break;
                case GpsDeviceInfo.GpsType.Serial:
                    picGpsIcon.Image = Properties.Resources.gps_serial;
                    break;
                default:
                    break;
            }
			
            lblGpsDeviceName.Text = gpsinfo.Name;
			SelectedChanged(false);
		}

		#region Implementation of IExtendedListItem

		public void SelectedChanged(bool isSelected)
		{
			if(isSelected)
			{
				this.BackColor = Color.CornflowerBlue;
			}
			else
			{
				this.BackColor = Color.WhiteSmoke;
			}
		}

		public void PositionChanged(int index)
		{
			//throw new System.NotImplementedException();

		}

		#endregion

		private void InitializeComponent()
		{
            this.picGpsIcon = new LetsDoIt.Controls.PictureBoxEx();
            this.lblGpsDeviceName = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // picGpsIcon
            // 
            this.picGpsIcon.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)));
            this.picGpsIcon.BackColor = System.Drawing.Color.Silver;
            this.picGpsIcon.Location = new System.Drawing.Point(3, 3);
            this.picGpsIcon.Name = "picGpsIcon";
            this.picGpsIcon.Size = new System.Drawing.Size(60, 60);
            this.picGpsIcon.TabIndex = 0;
            // 
            // lblGpsDeviceName
            // 
            this.lblGpsDeviceName.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Regular);
            this.lblGpsDeviceName.Location = new System.Drawing.Point(69, 14);
            this.lblGpsDeviceName.Name = "lblGpsDeviceName";
            this.lblGpsDeviceName.Size = new System.Drawing.Size(362, 37);
            this.lblGpsDeviceName.Text = "datetime";
            // 
            // GpsDeviceItem
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.Controls.Add(this.lblGpsDeviceName);
            this.Controls.Add(this.picGpsIcon);
            this.Name = "GpsDeviceItem";
            this.Size = new System.Drawing.Size(434, 66);
            this.ResumeLayout(false);

		}
		
		public new void Dispose()
		{
			picGpsIcon.Dispose();
		}

		int previousClick = 0; // ???? huh? SystemInformation.DoubleClickTime;

		protected override void OnClick(EventArgs e)
		{
			//base.OnClick(e);
			// This is really dumb. We want the event time, not the current
			// time!
			int now = System.Environment.TickCount;
			// A double-click is detected if the the time elapsed
			// since the last click is within DoubleClickTime.
			if (now - previousClick <= SystemInformation.DoubleClickTime)
			{
				// Raise the DoubleClick event.
				if (DoubleClick != null)
					DoubleClick(this, EventArgs.Empty);
				previousClick = 0;
			}
			else
			{
				// Set previousClick to now so that
				// subsequent double-clicks can be detected.
				previousClick = now;
				// Allow the base class to raise the regular Click event.
				base.OnClick(e);
			}
		}

		// Event handling code for the DoubleClick event.

		public new event EventHandler DoubleClick;
		protected new virtual void OnDoubleClick(EventArgs e)
		{
			if (this.DoubleClick != null)
				this.DoubleClick(this, e);
		}

	}
}
